




//
//  ZAddAddressViewController.m
//  ZQLearn
//
//  Created by macfeel on 2019/6/11.
//  Copyright © 2019 macfeel. All rights reserved.
//

#import "ZAddAddressViewController.h"
#import "GFAddressPicker.h"
@interface ZAddAddressViewController ()<UITextFieldDelegate,UITableViewDelegate,UITableViewDataSource,GFAddressPickerDelegate>

@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSArray *nameArray;

@property(nonatomic,strong)NSMutableArray *dataArray;

@property (nonatomic,strong)GFAddressPicker *pickerView;


@property (nonatomic,strong)UITextField *textField;

@property (nonatomic,copy)NSString *province;
@property (nonatomic,copy)NSString *city;
@property (nonatomic,copy)NSString *area;
@property (nonatomic,copy)NSString *address;

@property (nonatomic,copy)NSString *name;
@property (nonatomic,copy)NSString *phone;
//@property (nonatomic,copy)NSString *address;



@end

@implementation ZAddAddressViewController
#define cellIdentifier @"addadd"
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 0, ScreenWidth , ScreenHeight - NavBarHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 55.f;
        _tableView.backgroundColor = kBackgroundColor;
//        [_tableView registerNib:[UINib nibWithNibName:@"AddressCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];
        WeakStament(ws);
        //        _tableView.mj_header = [CustomRefreshHeader headerWithRefreshingBlock:^{
        //            [ws refreshData];
        //        }];
        //        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        //            [ws loadMoreData];
        //        }];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
     [self.navigationItem setItemWithTitle:@"添加收货地址" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    [self addNavgationRightButtonWithFrame:CGRectMake(0, 0, 35, 35) title:@"确认添加" Image:@"" selectedIMG:@"" tartget:self action:@selector(confirmAdd)];
    self.dataArray = [NSMutableArray array];
    self.nameArray = @[@"收货人：",
                       @"手机号：",
                       @"地址：",
                       @"详细地址：",
                       ];
    [self.view addSubview:self.tableView];
    // Do any additional setup after loading the view.
}
///确认添加

-(void)confirmAdd{
    
    NSDictionary *dict = @{@"province":self.province?:@"",@"city":self.city?:@"",@"area":self.area?:@"",@"userInfo.id":[UserInfoSave shareInstance].uid?:@"",@"address":self.address?:@"",@"phone":self.phone?:@"",@"name":self.name?:@"",@"isDefault":@"0"};
    [ZNetworkService postRequestWithBaseUrl:kAddressUrl setParameters:dict success:^(id result) {
        
        YYLog(@"%@",result);
        
        if ([result[@"errorCode"]integerValue]==0) {
            
            [AFToast showText:@"添加成功"];
            
        }
    } fail:^(NSError *error) {
        
    } delegater:nil];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.nameArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    cell.textLabel.text = self.nameArray[indexPath.row];
    if (indexPath.row!=2) {
         [self addTextFieldWithCell:cell withIndex:indexPath];
    }else{
        UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth-220, 15, 200, 30)];
        [cell.contentView addSubview:lab];
        lab.text = [UserInfoSave shareInstance].area?:@"";
    }
   
    cell.selectionStyle = 0;
    return cell;
}

-(void)addTextFieldWithCell:(UITableViewCell *)cell withIndex:(NSIndexPath*)indexPath{
    
    UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(ScreenWidth-220, 15, 200, 30)];
//    text.backgroundColor = kMagentaColor;
    if (indexPath.row==1) {
        text.keyboardType = UIKeyboardTypePhonePad;
    }
    text.textAlignment = 2;
    text.tag = indexPath.row;
    text.delegate = self;
    self.textField = text;
    [cell.contentView addSubview:text];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    [self.textField resignFirstResponder];
    //退键盘
    [self.view endEditing:YES];
    if (indexPath.row==2) {
        //地区选择器
        self.pickerView = [[GFAddressPicker alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-NavBarHeight)];
        [self.pickerView updateAddressAtProvince:@"北京市" city:@"北京市" town:@"东城区"];
        self.pickerView.delegate = self;
        self.pickerView.font = [UIFont boldSystemFontOfSize:18];
        [self.view addSubview:self.pickerView];
    }
    
}


#pragma mark - text delegate
-(void)textFieldDidEndEditing:(UITextField *)textField{
    
    if (textField.tag==0) {
        self.name = textField.text;
    }else if (textField.tag==1){
        self.phone = textField.text;
    }else{
        self.address = textField.text;
    }
    
}


#pragma mark - 地区选择器delegate

- (void)GFAddressPickerCancleAction
{
    [self.pickerView removeFromSuperview];
}

- (void)GFAddressPickerWithProvince:(NSString *)province
                               city:(NSString *)city area:(NSString *)area
{
    
    [self.pickerView removeFromSuperview];
    YYLog(@"%@  %@  %@",province,city,area);
    self.province = province;
    self.city = city;
    self.area = area;
    [UserInfoSave shareInstance].area = [NSString stringWithFormat:@"%@ %@ %@",province,city,area];
    
    ///刷新某一行
    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:2 inSection:0];
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
}


@end
